package utility;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtil {
	private static String SMTP_AUTH_USER = "MI.Team.Son";
	private static String SMTP_AUTH_PWD = "\u0062\u006F\u006E\u0067\u0064\u0061\u0073\u006F\u002E\u0063\u006F\u006D";
	public static boolean sendMailAuth(String to, String userName, String password){
		boolean result = false;
		
		String from = SMTP_AUTH_USER;
		String subject = "Login MI-Shop: Human Resource Management";
		// Assuming you are sending email from localhost
		
		// Get system properties
		Properties props = System.getProperties();
	
		// Setup mail server
		props.setProperty("mail.transport.protocol","smtp");
		props.setProperty("mail.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.quitwait", "false");
	
		// Get the default Session object.
		Session session = Session.getDefaultInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);				
			}
		});
		
		 try{
	         // Create a default MimeMessage object.
	         MimeMessage message = new MimeMessage(session);

	         // Set From: header field of the header.
	         message.setFrom(new InternetAddress(from));

	         // Set To: header field of the header.
	         message.addRecipient(Message.RecipientType.TO,
	                                  new InternetAddress(to));

	         // Set Subject: header field
	         message.setSubject(subject);
	         
	         // Set Date
	         message.setSentDate(new Date());

	         // Now set the actual message
	         MimeBodyPart msgHeader = new MimeBodyPart();
	         msgHeader.setText("Đăng Nhập Hệ Thống QLNS", "utf-8");
	         MimeBodyPart msgPart1 = new MimeBodyPart();
	         msgPart1.setText(" - Account: "+userName, "utf-8");
	         MimeBodyPart msgPart2 = new MimeBodyPart();
	         msgPart2.setText(" - Password: "+password+" (8 kí tự)", "utf-8");
	         MimeBodyPart msgPartNote = new MimeBodyPart();
	         msgPartNote.setText("Bạn có thể thay đổi password sau khi đăng nhập.", "utf-8");
	         MimeBodyPart msgPartFooter = new MimeBodyPart();
	         msgPartFooter.setText("Thanks, MI-TEAM !!", "utf-8");
	         
	         MimeMultipart multipart = new MimeMultipart();
	         multipart.addBodyPart(msgHeader);
	         multipart.addBodyPart(msgPart1);
	         multipart.addBodyPart(msgPart2);
	         multipart.addBodyPart(msgPartNote);
	         multipart.addBodyPart(msgPartFooter);	         
	         
	         message.setContent(multipart);
	         
	         // Send message
	         Transport.send(message);
	         result = true;
	         return result;
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	         return result;
	      }
	   }
}
